package com.situ.hotel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.hotel.entity.Admin;
import com.situ.hotel.mapper.AdminMapper;
import com.situ.hotel.service.AdminService;
import com.situ.hotel.util.MD5Util;
@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminMapper adminMapper;

	@Override
	public Admin login(Admin admin) throws Exception {
		// TODO Auto-generated method stub
		if (admin.getUsername() == null || admin.getUsername().length() < 3 ||

				admin.getUsername().length() > 16) {

			throw new Exception("账号为3-16为字符串");

		}

		if (admin.getPassword() == null || admin.getPassword().length() < 3 ||
				admin.getPassword().length() > 16) {
			throw new Exception("密码为3-16位字符串");
		}
		Admin sAdmin = adminMapper.selectByUsername(admin.getUsername());
		if (sAdmin == null) {
              throw new Exception("账号不存在");
		}
		
		if (!sAdmin.getPassword().equals(MD5Util.getMD5(admin.getPassword()))) {
			throw new Exception("密码错误");
		}

		return sAdmin;
	}

}
